﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.SharePoint.WebControls;
using System.Data;
using Data.HotSite.Properties;


namespace Presentation.HotSite.WebPart.VWPInscritos
{
    public partial class VWPInscritosUserControl : UserControl
    {

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #region Button Controls

        protected void btnInscritos_Click(object sender, EventArgs e)
        {
            CarregarListaInscritos();
        }

        #endregion


        #region Métodos

        private void CarregarListaInscritos()
        {
            string htmlConteudo = string.Empty;
            string[] arrayItensList = checkBoxInscritos.Items.Cast<ListItem>()
                .Where(i => i.Selected == true)
                    .Select(i => i.Value).ToArray();

           

            SPSite mySite = SPContext.Current.Site;
            SPWeb myWeb = mySite.OpenWeb();
            SPList myList = myWeb.Lists[SharepointStrings.ListaInscritos];
            var collectionInscritos = myList.GetItems(arrayItensList).Cast<SPListItem>().ToList();

            if (arrayItensList.Count() == 0)
            {
                htmlConteudo += "<table border='2'  style='display:none'><tr>";
            }

            if (arrayItensList.Count() > 0)
            {
                htmlConteudo += "<table border='2'><tr>";
            }


            foreach (string item in arrayItensList)
            {
                var itemName = checkBoxInscritos.Items.Cast<ListItem>().Where(i => i .Value.Equals(item)).Select(i => new { Text = i.Text }).ToList();
                htmlConteudo += string.Format(@"<td><b>{0}</b></td>", itemName[0].Text);                
            }


            htmlConteudo += "</tr>";
            foreach (SPListItem itemLista in collectionInscritos)
            {
                htmlConteudo += "<tr>";
                foreach (string item in arrayItensList)
                {

                    if (itemLista.Fields[item].Type == SPFieldType.Lookup)
                    {
                        //var spLookupValue = new SPFieldLookupValue(itemLista[item].ToString());
                        htmlConteudo += string.Format("<td>{0}</td>", itemLista[item].ToString().TrimStart(';', '#').Replace(";#", " <br/> -"));
                    }

                    else if (itemLista.Fields[item].Type == SPFieldType.Boolean)
                    {
                        //var spBoolean = new SPFieldBoolean(itemLista.Fields, itemLista.Fields[item].StaticName);
                        htmlConteudo += string.Format("<td>{0}</td>", itemLista[item].ToString() == "true" ? "Sim" : "Não");
                    }
                    else
                    {
                        htmlConteudo += string.Format("<td>{0}</td>", itemLista[item]);
                    } 
                }

                htmlConteudo += "</tr>";

            }
            htmlConteudo += "</table>";
            divTable.InnerHtml = htmlConteudo;
        }

        #endregion 

    }
}